package com.chuangke.file.controller;

import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.chuangke.common.constant.FileConstants;
import com.chuangke.common.http.HttpResult;
import com.chuangke.file.service.FileTransferService;

/**
 * @Description
 * @author stm
 * @date 2019年3月3日 上午10:04:12
 */
@RestController
@RequestMapping("image")
public class ImageController {

	@Autowired
	private FileTransferService fileService;
	
	@PostMapping("/upload")
	public HttpResult uploadImage(MultipartFile file) {
		return HttpResult.ok(fileService.upload(file,FileConstants.SYSTEM_DBANK_ID));
	}
	
	@GetMapping("{imageid}/{signid}")
	public void downloadFile(@PathVariable String imageid,@PathVariable String signid, HttpServletResponse response) {
		OutputStream os = null;
		try {
			os = response.getOutputStream();
			byte[] data = fileService.download(imageid,signid);
			if (data != null) {
				os.write(data, 0, data.length);
			}
			os.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
